package com.tang.auth.domain.service.impl;

import com.tang.auth.domain.entity.AuthRolePermissionBO;
import com.tang.auth.domain.service.AuthRolePermissionDomainService;
import com.tang.auth.infra.basic.entity.AuthRolePermission;
import com.tang.auth.infra.basic.service.AuthRolePermissionService;
import com.tang.common.enums.DeleteEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {
    @Resource
    private AuthRolePermissionService authRolePermissionService;

    /**
     * 添加权限
     *
     * @param authRolePermissionBO 添加权限
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/15 14:25
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("com.tang.auth.domain.service.impl.AuthRolePermissionDomainServiceImpl.add.bo:{}", authRolePermissionBO);
        }
        List<AuthRolePermission> rolePermissionList = new ArrayList<>();
        Long roleId = authRolePermissionBO.getRoleId();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDeleted(DeleteEnum.UN_DELETED.getCode());
            rolePermissionList.add(authRolePermission);
        });
        authRolePermissionService.batchInsert(rolePermissionList);
        return true;
    }
}
